// 订单路由
const express = require("express");
// 引入connect-multiparty中间件
var multipart = require("connect-multiparty");
// 创建中间件对象
var multipartMiddleware = multipart();
// 引入数据库配置
const db = require("../mysql");
// 创建路由对象
const order = express.Router();

// 获取订单
order.get('/getOrder', multipartMiddleware, (req, res) => {
    const sqlStr = `select * from orders where order_id like '%${req.query.query}%' or order_date like '%${req.query.query}%'  or order_seller like '%${req.query.query}%'  or order_buyer like '%${req.query.query}%'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '获取订单失败' });
        res.json({ code: 200, data: results });
    });
})

// 获取下的订单
order.get('/getBuyOrder/:username', multipartMiddleware, (req, res) => {
    const sqlStr = `select * from orders where order_username = '${req.params.username}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '获取购买订单信息失败' });
        res.json({ code: 200, data: results });
    });
})
// 获取收到的订单
order.get('/getSellOrder/:username', multipartMiddleware, (req, res) => {
    console.log(req.params)
    const sqlStr = `select * from orders where order_seller = '${req.params.username}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '获取出售订单信息失败' });
        res.json({ code: 200, data: results });
    });
})

// 根据订单id获取订单
order.get('/getOrderById/:orderId', multipartMiddleware, (req, res) => {
    console.log(req.params)
    const sqlStr = `select * from orders where order_id = '${req.params.orderId}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '获取订单失败' });
        res.json({ code: 200, data: results });
    });
})

// 添加订单信息
order.post('/addOrder', multipartMiddleware, (req, res) => {
    const sqlStr = `insert into orders (order_id,order_buyer,order_date,order_price,order_seller,order_status,order_book_cover,order_book_name,order_username,order_num)  values ('${req.body.order_id}','${req.body.order_buyer}','${req.body.order_date}','${req.body.order_price}','${req.body.order_seller}','${req.body.order_status}','${req.body.order_book_cover}','${req.body.order_book_name}','${req.body.order_username}','${req.body.order_num}')`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '添加订单失败' });
        return res.json({ code: 200, message: '添加订单成功' })
    })
})

// 设置订单状态
order.put('/updateOrder', multipartMiddleware, (req, res) => {
    const sqlStr = `update orders set order_status='${req.body.order_status}' where order_id='${req.body.order_id}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '修改订单状态失败' });
        return res.json({ code: 200, message: '修改订单状态成功' })
    })
})

// 删除订单
order.post("/deleteOrder", multipartMiddleware, (req, res) => {
    const sqlStr = `delete from orders where order_id='${req.body.order_id}'`;
    db.query(sqlStr, (err, results) => {
      if (err) return res.json({ code: 404, message: "删除失败" });
      res.json({ code: 200, message: "删除成功" });
    });
});
  
module.exports = order
